import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbcurriculum } from '../../../atoms/db';

const logger = anylogger('pages/api/courses/query.api');

export type IData = ITbcurriculum

type Result = {
	ok: true;
	data: IData[]
} | {
	ok: false;
	message: string;
};

export type Message = {
	semster: string;
	coursename: string

}

/**
 * pages/api/courses/query.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { coursename, semster } = req.body as Message;
		if (!coursename || !semster) {
			res.status(200).json({
				ok: false,
				message: '错误'
			});
			return;
		}
		const d = await tbcurriculum()
			.select('*')

			.orderBy('coursename',);
		logger.debug('data:', d);
		const data = d.map((it) => {
			return {
				...it
			} as IData;
		});

		res.status(200).json({
			ok: true,
			data
		});
	} catch (error) {
		logger.trace(error);
		res.status(200).json({
			ok: false,
			message: 'cuowu'
		});
	}
});

export const config = {} as PageConfig;

export default handler;
